//
//  LYMineFriendsController.m
//  artist
//
//  Created by Jay_Apple on 16/5/5.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYMineFriendsController.h"
#import "LYChatFriendsModel.h"
#import "LYMineFriendCell.h"
#import "ChatViewController.h"
#import "LYStuwebviewcontroller.h"
#import "LYTeacherWebViewController.h"
#import "LYOrzWebViewController.h"
@interface LYMineFriendsController ()<UITableViewDelegate, UITableViewDataSource, UIActionSheetDelegate>

@property (nonatomic, strong) NSMutableArray *List;
@property (nonatomic, strong) LYChatFriendsModel *selectedFriend;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation LYMineFriendsController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getFriends];
    
    self.tableView.tableFooterView = [[UIView alloc] init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 网络请求
/**
 *  拿到好友列表
 */
- (void)getFriends{
    
    [MBProgressHUD showMessage:nil];
    [LYRequestTool acquirFriendssWithToken:LYToken success:^(NSArray<LYChatFriendsModel *> *friends) {
        [MBProgressHUD hideHUD];
        self.List = [friends mutableCopy];
        [self.tableView reloadData];
        
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showOnlyText:message];
    }];
    
}

/**
 *  删除好友
 */
- (void)deleteFriendWithFriendID:(NSString *)friendID andIndexPath:(NSIndexPath *)indexPath{
    
    [LYRequestTool deleteFriendssWithToken:LYToken userID:friendID success:^{
        
        //移除模型数据
        [self.List removeObjectAtIndex:indexPath.row];
        //删除对应行
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    } failure:^(NSString *code, NSString *message) {
        
    }];

}

#pragma mark - tableView
/**
 *  设置tableview行数
 *
 *
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    //通过模型数组返回对应行数
    return self.List.count;
    
    
}

/**
 *  设置cell
 *
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"minefriendcell";
    
    LYMineFriendCell *cell = [LYMineFriendCell cellWithTableView:tableView andIndetifier:ID];
    
    //拿到数组模型
    LYChatFriendsModel *model = self.List[indexPath.row];
    
    cell.model = model;
    
    return cell;
}



- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    

    LYChatFriendsModel *model = self.List[indexPath.row];
    
    //判断编辑模式是否为delete
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self deleteFriendWithFriendID:model.user_id andIndexPath:indexPath];

    }
}

/**
 *  选中cell跳转编辑页面
 *
 */

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.selectedFriend = self.List[indexPath.row];

    
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"聊天",@"查看资料", nil];
    sheet.actionSheetStyle = UIActionSheetStyleAutomatic;
    [sheet showInView:self.view];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 65;
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != actionSheet.cancelButtonIndex) {
        
        NSString* button = [actionSheet buttonTitleAtIndex:buttonIndex];
        if ([button isEqualToString:@"聊天"]) {
            ChatViewController *cvc = [[ChatViewController alloc] initWithSessionId:self.selectedFriend.voip_account];
            cvc.friendModel = self.selectedFriend;
            cvc.title = [[DemoGlobalClass sharedInstance] getOtherNameWithPhone:self.selectedFriend.voip_account];
            [self.navigationController pushViewController:cvc animated:YES];
        } else if ([button isEqualToString:@"查看资料"]) {
            
            UIViewController *friendVC = nil;
            
            switch (self.selectedFriend.role_id) {
                case roleTypeStu:
                   friendVC = [LYStuWebViewController stuWebVCWithID:self.selectedFriend.user_id];
                    break;
                    
                case roleTypeTeacher:
                    friendVC = [LYTeacherWebViewController teaWebVCWithID:self.selectedFriend.user_id];
                    break;
                    
                case roleTypeOrz:
                    friendVC = [LYOrzWebViewController orzWebVCWithID:self.selectedFriend.user_id];
            }
            
            [self.navigationController pushViewController:friendVC animated:YES];

        }
    }
}


@end
